package com.liuya.common;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.i18n.LocaleContextHolder;

import java.io.FileInputStream;
import java.text.MessageFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;

/**
 * 国际化实现，也可使用Resource类。
 */
public class I18N {
    private static ResourceBundle rb;
    private static Locale locale;
    private static I18N instance;
    private static List locations;

    public void setLocations(List locations) {
        this.locations = locations;
    }

    public I18N() {
    }

    public I18N(List locations) {
        this.locations = locations;
        init();
    }

    private I18N(ResourceBundle rb) {
        this.rb = rb;
    }

    public static I18N getInstance() {
        if(instance != null)
            return instance;
        if(locale == null)
            locale = LocaleContextHolder.getLocale();
        ResourceBundle bundle = ResourceBundle.getBundle("i18n/Privilege_Web", locale);
        instance = new I18N(bundle);
        return instance;
    }

    public static I18N getWebInstance(HttpServletRequest req) {
        locale = req.getLocale();
        ResourceBundle bundle = ResourceBundle.getBundle("i18n/Privilege_Web", locale);
        return new I18N(bundle);
    }

    public static String getValidateMessageFile() {
        return getValidateMessageFile("messages");
    }

    public static String getValidateMessageFile(String baseName) {
        String country = locale.getCountry();
        if (country != null) {
            return baseName + "_" + country.toLowerCase() + ".js";
        } else {
            return "";
        }
    }

    public static String getValidateMessageFile(HttpServletRequest req, String baseName) {
        String country = req.getLocale().getCountry();
        if (country != null) {
            return baseName + "_" + country.toLowerCase() + ".js";
        } else {
            return "";
        }
    }

    /**
     * 返回国家的编码。如:cn
     * @return
     */
    public static String getCountry() {
        String country = locale.getCountry();
        if (country != null) {
            return country.toLowerCase();
        } else {
            return "";
        }
    }

    public static String say(String msg) {
        return rb.getString(msg);
    }

    public static String getAuthMessage(Locale locale, String key) {
        return ResourceBundle.getBundle("i18n/Auth", locale).getString(key);
    }

    public static String getAuthMessage(Locale locale, String key, String argument) {
        ResourceBundle rb = ResourceBundle.getBundle("i18n/Auth", locale);
        return MessageFormat.format(rb.getString(key), new Object[]{argument});
    }

    private static Map<String, String> ctxPropertiesMap;
    static Properties props = new Properties();

    public static void init(){
        for(int i=0; i< locations.size(); i++){
            String file = (String)locations.get(i) + "_" + WebUtil.getClientLanguage();
            try{
                props.load(new FileInputStream(file));

            } catch(Exception e) {
                System.out.println("读取国际化文件出错：" + e.getMessage());
            }
        }
        System.out.println(props.getProperty("myadf.dfdf"));//messages
        System.out.println(props.getProperty("policyAdminDescription"));//Auth
        System.out.println(props.getProperty("Add_expression_group"));//Privilege_Web
        System.out.println(props.getProperty("EMAIL.SET_HOST"));//messages
    }

    public static String getMessage(String key) {
        try {
            return ctxPropertiesMap.get(key);
        } catch (MissingResourceException e) {
            return null;
        }
    }

    public static String getMessage(String key, Object... argument) {
        try {
            return MessageFormat.format(getMessage(key), new Object[]{argument});
        } catch (MissingResourceException e) {
            return null;
        }
    }
}
